package cc.yugu.yhdw.webserver.service.basic;

import cc.yugu.yhdw.common.constant.ConstEnum;
import cc.yugu.yhdw.common.domain.basic.CustomerForegiftOrder;
import cc.yugu.yhdw.common.domain.basic.CustomerInOutMoney;
import cc.yugu.yhdw.common.entity.json.ExtResult;
import cc.yugu.yhdw.common.entity.pagination.Page;
import cc.yugu.yhdw.webserver.persistence.basic.CustomerForegiftOrderMapper;
import cc.yugu.yhdw.webserver.persistence.basic.CustomerInOutMoneyMapper;
import cc.yugu.yhdw.webserver.persistence.basic.PassportMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
/**
 * 用户押金记录业务
 *
 * @Date:Created in 2018/7/3 14:37
 * @Author:
 * @Modified By: zjn 2018/7/3 14:37
 */
@Service
public class CustomerForegiftOrderService {
    @Autowired
    CustomerForegiftOrderMapper customerForegiftOrderMapper;
    @Autowired
    CustomerInOutMoneyMapper customerInOutMoneyMapper;
    @Autowired
    PassportMapper customerMapper;
    /**
    * 分页查询
    *
    * @Param: [search]
    * @Return: cc.yugu.yhdw.common.entity.pagination.Page
    * @Author By: zjn 2018/7/3  14:44
    */
    public Page findPage(CustomerForegiftOrder search) {
        Page page = search.buildPage();
        /*
          新增加运营商代理商登录筛选
         */
        if(search.getAgentId()!=null&&search.getAgencyId()==null){

            page.setTotalItems(customerForegiftOrderMapper.findPageCounts(search));
            search.setBeginIndex(page.getOffset());
            page.setResult(customerForegiftOrderMapper.findPageResults(search));
            return page;
        }
        if(search.getAgentId()!=null&&search.getAgencyId()!=null){
            page.setTotalItems(customerForegiftOrderMapper.findPageCounts(search));
            search.setBeginIndex(page.getOffset());
            page.setResult(customerForegiftOrderMapper.findPageResults(search));
            return page;
        }
        // 总部
        if(search.getAgentId()==null&&search.getAgencyId()==null){
            page.setTotalItems(customerForegiftOrderMapper.findPageCount(search));
            search.setBeginIndex(page.getOffset());
            page.setResult(customerForegiftOrderMapper.findPageResult(search));
            return page;
        }
        return null;
    }

    /**
    * 根据主键id查询表bas_customer_foregift_order中押金信息
    *
    * @Param: [id]
    * @Return: cc.yugu.yhdw.common.domain.basic.CustomerForegiftOrder
    * @Author By: zjn 2018/7/3  14:44
    */
    public CustomerForegiftOrder find(String id) {
        return customerForegiftOrderMapper.find(id);
    }

    /**
    * 退款
    *
    * @Param: [userName, customerForegiftOrder]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/3  14:45
    */
    @Transactional(rollbackFor=Throwable.class)
    public ExtResult refund(String userName, CustomerForegiftOrder customerForegiftOrder) {
        // 根据主键查询表bas_customer_foregift_order中押金信息
        CustomerForegiftOrder order = customerForegiftOrderMapper.find(customerForegiftOrder.getId());
        if (order == null) {
            return ExtResult.failResult("订单不存在");
        }

        if (order.getStatus() != CustomerForegiftOrder.Status.APPLY_REFUND.getValue() ) {
            return ExtResult.failResult("只有申请退款状态才可退款！");
        }
        // 如果退款金额 大于 订单金额
        if (customerForegiftOrder.getRefundMoney() > order.getMoney()){
            return ExtResult.failResult("退款金额不能大于订单金额");
        }

        CustomerInOutMoney customerInOutMoney = new CustomerInOutMoney();
        // 设置客户id
        customerInOutMoney.setCustomerId(order.getCustomerId());
        // 设置支付类型
        customerInOutMoney.setBizType(CustomerInOutMoney.BizType.CUSTOMER_FOREGIFT_REFUND.getValue());
        // 设置订单号来源
        customerInOutMoney.setSourceId(order.getId());
        // 设置退款金额
        customerInOutMoney.setMoney(customerForegiftOrder.getRefundMoney());
        // 设置创建时间
        customerInOutMoney.setCreateTime(new Date());
        //插入到数据库
        customerInOutMoneyMapper.insert(customerInOutMoney);

        if(order.getPayType() == ConstEnum.PayType.BALANCE.getValue()) {
            customerMapper.updateBalance(order.getCustomerId(), customerForegiftOrder.getRefundMoney());
        }

        List<Integer> statusList = Arrays.asList(CustomerForegiftOrder.Status.PAY_OK.getValue(), CustomerForegiftOrder.Status.APPLY_REFUND.getValue());

        customerForegiftOrderMapper.updateStatus(customerForegiftOrder.getId(),
                CustomerForegiftOrder.Status.REFUND_SUCCESS.getValue(),
                customerForegiftOrder.getRefundMoney(), userName, new Date(),
                customerForegiftOrder.getRefundPhoto(),
                customerForegiftOrder.getMemo(), new Date());

        customerMapper.setOldBatteryForegiftOrderId(order.getCustomerId());
        customerMapper.clearBatteryForegiftOrderId(order.getCustomerId(), statusList);

        return ExtResult.successResult();
    }
    /**
    * 拒绝退款操作
    * 
    * @Param: [userName, customerForegiftOrder]
    * @Return: cc.yugu.yhdw.common.entity.json.ExtResult
    * @Author By: zjn 2018/7/3  15:01
    */ 
    @Transactional(rollbackFor=Throwable.class)
    public ExtResult repulseRefund(String userName, CustomerForegiftOrder customerForegiftOrder) {
        if(StringUtils.isEmpty(customerForegiftOrder.getMemo())){
            return ExtResult.failResult("取消退款原因不能为空");
        }
        // 设置订单状态
        customerForegiftOrder.setStatus(CustomerForegiftOrder.Status.PAY_OK.getValue());
        // 设置退款人
        customerForegiftOrder.setRefundOperator(userName);
        // 设置处理时间
        customerForegiftOrder.setHandleTime(new Date());
        int total = customerForegiftOrderMapper.update(customerForegiftOrder);
        if(total > 0) {

        } else  {
            return ExtResult.failResult("操作失败");
        }

        return ExtResult.successResult();
    }

}
